﻿using Item.Buff;
using LitJson;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Battle;

namespace ItemComponent
{
    /// <summary>
    /// 射线
    /// </summary>
    public class RayEmitter : Gun
    {
        protected const string Key_RayData = "RayData";

#if UNITY_EDITOR
        [SerializeField, ReadOnly]
        private ST_RayEmitterData debugRayData;
#endif
        public ST_RayEmitterData RayData { get; protected set; }
        public ST_RayEmitterData CurRayData { get; protected set; }

        protected Bullet_Ray bulletRay;
        [SerializeField,ReadOnly]
        protected float fRayPercent = 0;
        public float RayPercent
        {
            get => fRayPercent;
            protected set
            {
                fRayPercent = Mathf.Clamp(value, 0, (float)CurRayData.dMaxScale);

                bulletRay?.SetRayPercent(fRayPercent);
            }
        }

        public override void InitComponent(JsonData data, Item.ItemBase item)
        {
            if (data == null || data[Key_RayData] == null) return;

            ST_RayEmitterData tmp = JsonMapper.ToObject<ST_RayEmitterData>(data[Key_RayData].ToJson());

            RayData = tmp;
            CurRayData = tmp;
#if UNITY_EDITOR
            debugRayData = tmp;
#endif
            base.InitComponent(data,item);
        }

        public override void OnBuffChange(STBuffEffectData buffEffectData)
        {
            base.OnBuffChange(buffEffectData);

            var rayData = RayData;

            rayData.dPercentGrowth *= buffEffectData.fSpeed;
            rayData.dRayLength *= buffEffectData.fRange;
            rayData.dRayWidth *= buffEffectData.fRange;

            CurRayData = rayData;

            if (bulletRay != null)
            {
                bulletRay.InitBulletData(GetBulletData(EnemyMgr.Ins.GetTarget(transform.position)), null, null);
                bulletRay.SetRaySize((float)CurRayData.dRayLength, (float)CurRayData.dRayWidth);
            }

#if UNITY_EDITOR
            debugRayData = rayData;
#endif
        }

        protected override void Update()
        {
            base.Update();

            if (!bReload && bulletRay != null && bulletRay.RayActive)
            {
                RayPercent += Time.deltaTime * (float)CurRayData.dPercentGrowth;
            }
        }

        private void OnDisable()
        {
            if (bulletRay != null)
            {
                bulletRay.DestorySelf();
                bulletRay = null;
            }
        }

        protected override void Reload()
        {
            base.Reload();

            if (bulletRay != null)
                bulletRay.RayActive = false;
            RayPercent = 0;
        }
        protected override void OnReloadOver()
        {
            base.OnReloadOver();

            if (bulletRay != null)
            {
                bulletRay.RayActive = true;
                bulletRay?.SetTarget(EnemyMgr.Ins.GetTarget(transform.position));
            }
        }

        protected async override void FireAsync(ICanHitObj target)
        {
            if (bulletRay == null)
            {
               var tmp = await BulletFactory.CreateBulletAsync<Bullet_Ray>(GetBulletData(target), BulletFactory.RayPath);
                //防止异步导致生成无用的对象
                if (bulletRay != null)
                    tmp.DestorySelf();
                else
                    bulletRay = tmp;

                bulletRay?.SetRaySize((float)CurRayData.dRayLength, (float)CurRayData.dRayWidth);
            }

            bulletRay.SetTarget(target);
            bulletRay.Attack();
        }

#if UNITY_EDITOR
        public static STMyComponetData GetMyComponetData(ST_GunData gunData, ST_RayEmitterData rayEmitterData)
        {
            STMyComponetData componentData = GetMyComponetData(gunData);
            componentData.strType = typeof(RayEmitter).FullName;
            componentData.data[Key_RayData] = JsonMapper.ToObject(JsonMapper.ToJson(rayEmitterData));

            return componentData;
        }
#endif
    }
}